package com.httpdemo.http_teaching_server.controller;

import com.httpdemo.http_teaching_server.model.User;
import com.httpdemo.http_teaching_server.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/auth")
public class UserInfoController {

    @Autowired
    private UserRepository userRepository;

    @GetMapping("/user")
    public ResponseEntity<Map<String, String>> userInfo(@CookieValue("session") String sessionId) {
        Long userId = Long.parseLong(sessionId);
        User user = userRepository.findById(userId).orElse(null);
        if (user == null) {
            return ResponseEntity.status(401).build();
        }

        return ResponseEntity.ok(Map.of("username", user.getUsername()));
    }
}